package cn.hncu.booksManagementSystem.stock.dao.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import cn.hncu.booksManagementSystem.common.enums.XxxIdEnum;
import cn.hncu.booksManagementSystem.stock.dao.dao.StockDAO;
import cn.hncu.booksManagementSystem.stock.vo.StockModel;
import cn.hncu.booksManagementSystem.stock.vo.StockQueryModel;
import cn.hncu.booksManagementSystem.utils.FileIoUtil;
import cn.hncu.booksManagementSystem.utils.GeneratorXxxID_Util;
/**
 * CreateTime: 2018年4月19日 下午9:43:41	
 * @author 宋进宇  Email:447441478@qq.com
 * Description:
 *	StockDAO的实现类，通过文件存储
 */
public class StockDAO_FileImpl implements StockDAO{
	//数据源文件名
	private final String FILE_NAME = "./data/stock.info";
	
	@Override
	public boolean add(StockModel stock) {
		//如果stock为null，则返回false
		if (stock==null) {
			return false;
		}
		//读取所有数据
		Collection<StockModel> col = FileIoUtil.readFromDataBase(FILE_NAME);
		//查找stock是不是新的数据
		for (StockModel model : col) {
			//如果相同则返回false
			if (model.equals(stock)) {
				return false;
			}
		}
		//能到这里说明，是新数据
		boolean boo = col.add(stock);
		//产生下一条数据的id
		GeneratorXxxID_Util.updateXxxId(XxxIdEnum.STOCK);
		return boo && FileIoUtil.write2DataBase(col, FILE_NAME);
	}

	@Override
	public boolean updade(StockModel newStock) {
		//如果newStock为null，则返回false
		if (newStock == null) {
			return false;
		}
		//读取所有数据
		Collection<StockModel> col = FileIoUtil.readFromDataBase(FILE_NAME);
		List<StockModel> list = (List<StockModel>) col;
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).equals(newStock)) {
				return list.set(i, newStock) != null && FileIoUtil.write2DataBase(list, FILE_NAME);
			}
		}
		return false;
	}

	@Override
	public StockModel getStockInfoById(String id) {
		//如果id为null，则返回false
		if (id==null) {
			return null;
		}
		//读取所有数据
		Collection<StockModel> col = FileIoUtil.readFromDataBase(FILE_NAME);
		for (StockModel stock : col) {
			if (stock.getId().equals(id)) {
				return stock;
			}
		}
		return null;
	}

	@Override
	public Collection<StockModel> getAll() {
		return FileIoUtil.readFromDataBase(FILE_NAME);
	}

	@Override
	public Collection<StockModel> getStocksInfoByCondition(StockQueryModel sqm) {
		Collection<StockModel> col = getAll();
		if (sqm==null) {
			return col;
		}
		Collection<StockModel> resCol = new ArrayList<StockModel>();
		for (StockModel stock : col) {
			//卫条件1 查id
			//判断条件是否有效
			if (sqm.getId()!=null&&sqm.getId().trim().length()>0) {
				//如果不相等，跳过
				if(!stock.getId().trim().equals(sqm.getId().trim())) {
					continue ;
				}
			}
			
			//卫条件2 查bookId
			//判断条件是否有效
			if (sqm.getBookId()!=null&&sqm.getBookId().trim().length()>0) {
				//如果不相等，跳过
				if(!stock.getBookId().trim().equals(sqm.getBookId().trim())) {
					continue ;
				}
			}
			
			//卫条件3 查数量 范围查询
			//判断条件是否有效
			if (sqm.getSumNum()>0) {
				//小于小的跳过
				if (stock.getSumNum()<sqm.getSumNum()) {
					continue;
				}
			}
			//判断条件是否有效
			if (sqm.getSumNum2()>0) {
				//大于大的跳过
				if (stock.getSumNum()>sqm.getSumNum2()) {
					continue;
				}
			}
			
			resCol.add(stock);
		}
		return resCol;
	}


}
